<?php
class UsersController extends AppController {

	var $name = 'Users';
    
    function login() {
		//$user = $this->User->find('first', array('conditions'=>array('username'=>$this->data['User']['username'], 
          //      'password'=>md5($this->data['User']['password']))));
	}
	
	function logout() {
		$this->Session->destroy('curUser'); 
        $this->Session->setFlash('Bạn đã thoát ra thành công'); 
        $this->redirect(array('controller'=>'users','action'=>'login')); 
	}

	function index() {
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('user', $this->User->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->User->create();
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		$sinhviens = $this->User->Sinhvien->find('list');
		$giangviens = $this->User->Giangvien->find('list');
		$this->set(compact('sinhviens', 'giangviens'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid user', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->User->save($this->data)) {
				$this->Session->setFlash(__('The user has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->User->read(null, $id);
		}
		$sinhviens = $this->User->Sinhvien->find('list');
		$giangviens = $this->User->Giangvien->find('list');
		$this->set(compact('sinhviens', 'giangviens'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for user', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->User->delete($id)) {
			$this->Session->setFlash(__('User deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('User was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
    
    function doipass(){
        
        
       
		if (!empty($this->data)) {
            
            //$pcu=md5($this->data['User']['passwordcu']);
           $kt=$this->User->find('first',array(
            'conditions'=>array('password'=>$this->data['User']['passwordcu'],'id'=>$this->Auth->user('id')),
            'fields'=>array('User.id'),
            'recursive'=>-1));
            if(!empty($kt)){
                $this->User->id = $this->Auth->user('id');
  
                //$pass=md5($this->data['User']['password1']);
                $pass=$this->data['User']['password1'];
                if ($this->User->saveField('password', $pass)) {
				    $this->Session->setFlash(__('Mật khẩu đã được sửa', true));
				    $this->redirect(array('controller'=>'pages','action' => 'home'));
			     } else {
				    $this->Session->setFlash(__('Mật khẩu chưa được sửa, vui lòng thử lại', true),'error');
                    $this->redirect(array('action' => 'editp'));
			     }
            }else{
            	$this->Session->setFlash(__('Mật khẩu cũ chưa đúng', true));
                $this->redirect(array('action' => 'editp'));
            
            
            }
        }
        
    }
     function editp()
    {
	}
}
